const db = require('../Mysql/sql')
const dayjs = require('dayjs')

const follow = {
	//follow，news(关注表，消息表)单聊
	//根据用户id查询关注列表
	followList(req, res) {
		let {
			page,
			pageSize
		} = req.body
		const sql =
			`select f.*,u.username,u.userid from follow f left join users u on f.fanid = u.userid where f.userid=? limit ?,?`
		db.query(sql, [req.auth.userid, page, pageSize], (err, results) => {
			if (err) return res.cc(err)
			res.send({
				status: 200,
				data: results,
				mas: '获取成功'
			})
		})
	},
	//添加关注
	followAdd(req, res) {
		// select f.* from follow f where f.userid =  9 and f.fanid = 12
		let content = {
			...req.body,
			createtime: dayjs().format('YYYY-MM-DD HH:mm:ss')
		}
		const sql = 'insert into follow set ?'
		// const sql2 = `select f.* from follow f where f.userid =  ${req.auth.userid} and f.fanid = ${req.body.userid}` //判断是否互关
		db.query(sql, content, (err, results) => {
			if (err) return res.cc(err)
			res.cc('关注成功', 200)
		})
		
	},
	//取消关注
	followCancel(req, res) {
		const sql = `delete from follow where id=?`
		db.query(sql, req.body.id, (err, results) => {
			if (err) return res.cc(err)
			if (!results.affectedRows) return res.cc('取消失败')
			res.cc('取消成功', 200)
		})
	},

	//修改聊天信息
	followEdit(req,res) {
		const sql = `update follow set ? where id=?`
		db.query(sql, [req.body,req.body.id], (err, results) => {
			if (err) return res.cc(err)
			if (!results.affectedRows) return res.cc('取消失败')
			res.cc('取消成功', 200)
		})
	}
}
const crowd = {
	//crowd,crowd_menber,crowd_msg(群列表，群成员表，群消息表)
	getCrowdInfo(req,res) {
		// select *,if((c.id) in (select cm.crowd_id from crowd_menber cm where cm.userid = 13),true,false) as isJoin from crowd c;
		const sql = `select *,if((c.id) in (select cm.crowd_id from crowd_menber cm where cm.userid = ${req.auth.userid}),true,false) as isJoin from crowd c where c.title like '%${req.body.searchValue}%' or c.id like '%${req.body.searchValue}%'`
		db.query(sql, [req.auth.userid], (err, results) => {
			if (err) return res.cc(err)
			res.send({
				status: 200,
				data: results,
				mas: '获取成功'
			})
		})
	},
	//根据id获取群列表
	getCrowdList(req, res) {
		const sql = `select * from crowd c,crowd_menber cm where c.id = cm.crowd_id and cm.userid = ?
		${req.body.id?`and c.id=${req.body.id}`:''}
		`
		db.query(sql, [req.auth.userid], (err, results) => {
			if (err) return res.cc(err)
			res.send({
				status: 200,
				data: results,
				mas: '获取成功'
			})
		})
	},
	//解散群
	crowdDel(req, res) {
		const sql = `delete from crowd where id=? and leaderid=${req.auth.userid}`
		db.query(sql, req.body.id, (err, results) => {
			if (err) return res.cc(err)
			if (!results.affectedRows) return res.cc('删除失败')
			//删除成员列表对应的数据
			res.cc('删除成功', 200)
		})
	},
	//创建群
	crowdCreate(req, res) {
		let content = {
			...req.body,
			createtime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
			leaderid: req.auth.userid
		}
		const sql = 'insert into crowd set ?'
		db.query(sql, content, (err, results) => {
			if (err) return res.cc(err)
			res.send({
				status: 200,
				msg: '创建成功',
				data: results.insertId
			})
		})
	},
	//修改群
	crowdEdit(req, res) {
		// select * from crowd c,crowd_menber cm where c.id = cm.crowd_id
		const sql =
			`update crowd c join crowd_menber cm on c.id = cm.crowd_id and cm.userid = ${req.auth.userid} and cm.crowd_id=${req.body.crowd_id} set ?`
		db.query(sql, req.body, (err, results) => {
			if (err) return res.cc(err)
			if (!results.affectedRows) return res.cc('屏蔽失败')
			res.cc('修改成功', 200)
		})
	},
	//群成员列表
	crowdMenberList(req, res) {

		let sql = `select * from (select c.*,u.username,u.handimg,u.sex from crowd_menber c 
		left join users u on c.userid = u.userid) cm left join crowd c on cm.crowd_id = c.id 
		where cm.crowd_id = ? order by cm.userid=${req.body.leaderid} desc`
		if (req.body.searchValue) {
			sql = sql.concat(` and cm.username like '%${req.body.searchValue}%'`)
		}
		db.query(sql, [req.body.id], (err, results) => {
			if (err) return res.cc(err)
			res.send({
				status: 200,
				data: results,
				mas: '获取成功'
			})
		})
	},
	//加入群
	crowdAdd(req, res) {
		let {
			crowd_id,
			list
		} = req.body
		list.map(v => v.addtime = dayjs().format('YYYY-MM-DD HH:mm:ss'))
		let users = list.map((item, index) => {
			return Object.assign({}, {
				'userid': item.userid,
				"addtime": item.addtime
			})
		})
		let sql = 'insert into crowd_menber(userid,addtime,crowd_id) values '
		for (let i = 0; i < users.length; i++) {
			sql = sql.concat(`(${users[i].userid},'${users[i].addtime}',${crowd_id}),`)
		}
		sql = sql.toString().substring(0, sql.lastIndexOf(","))
		console.log(sql, 'sql')
		db.query(sql, [], (err, results) => {
			if (err) return res.cc(err)
			res.cc('加入成功', 200)
		})
	},
	//退群
	crowdLeave(req, res) {
		const sql = `delete from crowd_menber where id=? and userid=?`
		console.log(req.body)
		db.query(sql, [req.body.id,req.body.userid], (err, results) => {
			if (err) return res.cc(err)
			if (!results.affectedRows) return res.cc('退群失败')
			res.cc('退群成功', 200)
		})
	},
}

const msg = {
	//获取聊天信息（单聊）
	getChatMsg(req, res) {
		let {
			fanid,
			receiveid,
			pageSize,
			page
		} = req.body
		page = page - 1 || 0
		pageSize = pageSize * page || 1
		const sql = `select * from follow_msg where fanid=? and receiveid=? limit ?,?`
		db.query(sql, [fanid, receiveid, page, pageSize], (err, results) => {
			if (err) return res.cc(err)
			res.send({
				status: 200,
				msg: '获取成功',
				data: results
			})
		})
	},
	//保存单聊消息(服务端存储)
	saveChatMsg(req, res) {
		let body = {
			...req.body,
			createtiem: dayjs().format('YYYY-MM-DD HH:mm:ss')
		}
		const sql = `insert into follow_msg set ?`
		db.query(sql, req.body, (err, results) => {
			if (err) return res.cc(err)
			if (!results.affectedRows) return res.cc('存储失败')
		})
	},
	//撤回单聊消息
	recallChatMsg(req, res) {
		const sql = `delete from follow_msg where id=?`
		db.query(sql, req.body.id, (err, results) => {
			if (err) return res.cc(err)
			if (!results.affectedRows) return res.cc('撤回失败')
		})
	},
}

module.exports = {
	...follow,
	...crowd,
	...msg
}